#include "Ticker.h"
#include "us_ticker_api.h"

TickerBase::TickerBase(const ticker_data_t *data) : TimerEvent(data)
{
}

void TickerBase::detach()
{
    remove();
    _store_obj.obj = nullptr;
    _store_obj._method = nullptr;
}
void TickerBase::setup(us_timestamp_t t)
{
    remove();
    _us_delay = t;
    insert_absolute(_ticker_data.now() + t);
}

void TickerBase::setup_absolute(us_timestamp_t t)
{
    remove();
    insert_absolute(t);
}

void TickerBase::handler()
{
    insert_absolute(get_time_point(event) + _us_delay);
    if( _store_obj.obj){
        ((_model_function_object::_class*)(_store_obj.obj)->*(_store_obj._method))();
    }else if(_store_obj._func!=nullptr){
        _store_obj._func();
    }
}

Ticker::Ticker() : TickerBase(get_us_ticker_data())
{
}